<template>
	<view>
		<image src="../../static/登录页图片.png" style="width: 100%;" mode="widthFix"></image>
	</view>
	<view class="example" style="margin-top: 30px;">
		<!-- 基础表单校验 -->
		<uni-forms ref="valiForm" :model="valiFormData">
			<uni-forms-item label="用户名" name="userName">
				<uni-easyinput v-model="valiFormData.userName" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="userPassword">
				<uni-easyinput type="Password" v-model="valiFormData.userPassword" />
			</uni-forms-item>
		</uni-forms>
		<view style="margin-top: 80px;">
			<button type="primary" @click="submit('valiForm')">登录</button>
		</view>



	</view>
</template>

<script>
	import CryptoJS from 'crypto-js';
	import {
		jwtDecode
	} from 'jwt-decode';

	export default {
		data() {
			return {
				valiFormData: {
					userName: '',
					userPassword: '',
					userRoles: "",
				},

				loginData: {
					loginUserId: null,
					loginUserPassword: null,
				},
			}
		},
		onLoad() {},
		onReady() {},
		methods: {
			encrypt(plainText) {
				const CRYPTOJSKEY = "keyhlddz";
				var plaintText1 = plainText;
				var options = {
					mode: CryptoJS.mode.ECB,
					padding: CryptoJS.pad.Pkcs7
				};
				var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
				var encryptedData = CryptoJS.AES.encrypt(plaintText1, key, options);
				var encryptedBase64Str = encryptedData.toString().replace(/\//g, "_");
				encryptedBase64Str = encryptedBase64Str.replace(/\+/g, "-");
				return encryptedBase64Str;
			},
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex;
			},
			submit(ref) {
				// 使用加密后的密码进行登录请求
				const encryptedPassword = this.encrypt(this.valiFormData.userPassword);
				this.loginRequest(encryptedPassword);
			},

			loginRequest(encryptedPassword) {
				uni.request({
					url: 'http://localhost:8088/userLogin?loginUserId=' + this.valiFormData.userName +
						'&loginUserPassword=' + encryptedPassword,
					method: 'POST',
					success: (res) => {
						console.log('Login response:', res.data);
						if (res.data.code === '0') {
							const token = res.data.data;
							localStorage.setItem('token', res.data.data);
							this.loginData = jwtDecode(res.data.data);
							localStorage.setItem('userId', this.loginData.userId);
							localStorage.setItem('userName', this.loginData.userName);
							localStorage.setItem('userRoles', JSON.stringify(this.loginData.userRoles));
							localStorage.setItem('currentRole', this.loginData.userRoles[0].userRole);
							const userRoles = JSON.parse(localStorage.getItem("userRoles")); // 解析为数组
							const userRole = userRoles[0].userRole
							console.log(userRole);
							this.navigateToPageBasedOnRole(userRole);
						} else {
							console.error('Login failed:', res.data.msg);
						}
					},
					fail: (err) => {
						console.error('Login request failed:', err);
					},
				});
			},
			navigateToPageBasedOnRole(role) {
				switch (role) {
					case '综合办':
						uni.redirectTo({
							url: '/pages/generalHome/generalHome',
						});
						break;
					case '部门管理员':
						uni.redirectTo({
							url: '/pages/departmentHome/departmentHome',
						});
						break;
					case '驾驶员':
						uni.redirectTo({
							url: '/pages/driverHome/driverHome',
						});
						break;
					case '用车人':
						uni.redirectTo({
							url: '/pages/userHome/userHome',
						});
						break;
						// 添加其他角色的判断
					default:
						break;
				}
			},
		},
	}
</script>

<style>
	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		margin-left: 10px;
	}
</style>